package com.ruoyi.hxq_package.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.hxq_package.domain.PackageProduct;
import com.ruoyi.hxq_package.mapper.HxqPackageMapper;
import com.ruoyi.hxq_package.domain.HxqPackage;
import com.ruoyi.hxq_package.service.IHxqPackageService;

/**
 * 套餐管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-15
 */
@Service
public class HxqPackageServiceImpl implements IHxqPackageService 
{
    @Autowired
    private HxqPackageMapper hxqPackageMapper;

    /**
     * 查询套餐管理
     * 
     * @param id 套餐管理主键
     * @return 套餐管理
     */
    @Override
    public HxqPackage selectHxqPackageById(Long id)
    {
        return hxqPackageMapper.selectHxqPackageById(id);
    }

    /**
     * 查询套餐管理列表
     * 
     * @param hxqPackage 套餐管理
     * @return 套餐管理
     */
    @Override
    public List<HxqPackage> selectHxqPackageList(HxqPackage hxqPackage)
    {
        return hxqPackageMapper.selectHxqPackageList(hxqPackage);
    }

    /**
     * 新增套餐管理
     * 
     * @param hxqPackage 套餐管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHxqPackage(HxqPackage hxqPackage)
    {
        hxqPackage.setCreateTime(DateUtils.getNowDate());
        int rows = hxqPackageMapper.insertHxqPackage(hxqPackage);
        insertPackageProduct(hxqPackage);
        return rows;
    }

    /**
     * 修改套餐管理
     * 
     * @param hxqPackage 套餐管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHxqPackage(HxqPackage hxqPackage)
    {
        hxqPackageMapper.deletePackageProductByPackageId(hxqPackage.getId());
        insertPackageProduct(hxqPackage);
        return hxqPackageMapper.updateHxqPackage(hxqPackage);
    }

    /**
     * 批量删除套餐管理
     * 
     * @param ids 需要删除的套餐管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHxqPackageByIds(Long[] ids)
    {
        hxqPackageMapper.deletePackageProductByPackageIds(ids);
        return hxqPackageMapper.deleteHxqPackageByIds(ids);
    }

    /**
     * 删除套餐管理信息
     * 
     * @param id 套餐管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHxqPackageById(Long id)
    {
        hxqPackageMapper.deletePackageProductByPackageId(id);
        return hxqPackageMapper.deleteHxqPackageById(id);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param hxqPackage 套餐管理对象
     */
    public void insertPackageProduct(HxqPackage hxqPackage)
    {
        List<PackageProduct> packageProductList = hxqPackage.getPackageProductList();
        Long id = hxqPackage.getId();
        if (StringUtils.isNotNull(packageProductList))
        {
            List<PackageProduct> list = new ArrayList<PackageProduct>();
            for (PackageProduct packageProduct : packageProductList)
            {
                packageProduct.setPackageId(id);
                list.add(packageProduct);
            }
            if (list.size() > 0)
            {
                hxqPackageMapper.batchPackageProduct(list);
            }
        }
    }
}
